package com.webServices;
import com.databaseManager.DatabaseFunc;
import com.databaseManager.Doctor;

import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

// 扩展 HttpServlet 类
@WebServlet(name = "Hello")
public class Hello extends HttpServlet {
    private Doctor user;
    private DatabaseFunc df;
    public void init() throws ServletException
    {
        // 执行必需的初始化
        user = null;
        df = new DatabaseFunc();
    }

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
            throws ServletException, IOException
    {
        // 设置响应内容类型
        response.setContentType("text/html;charset=utf-8");

        String Eid = request.getParameter("Eid");
        String password = request.getParameter("Password");
        Doctor tmp = null;
        try {
            tmp = df.findDoctor(Eid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 实际的逻辑是在这里
        if(tmp != null && tmp.getPassword().equals(password))
        {
            user = tmp;
            response.setStatus(302);
            response.setHeader("Location", "manage.html");
        }
        else
        {
            PrintWriter out = response.getWriter();
            out.print("<script>alert(\"用户名或密码错误！\")</script>");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }


    public void destroy()
    {
        // 什么也不做
    }
}